<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
    <link rel="stylesheet" href="layuiadmin/plugins/input-tag/inputTag.css">
    <script src="layuiadmin/plugins/input-tag/inputTag.js"></script>
<script th:src="@{/}+'js/tool.js'"></script>
  <style type="text/css">
	.layui-form .layui-form-item{
		width:90%;
	}
	.layui-input-inline{
		width:350px!important
	}
	</style>

</head>
<body style="margin-top: 100px">
<form class="layui-form" lay-filter="myform" id="myform">
<input type="hidden" name="state" value="1">
<input type="hidden" name="tag" id="tag">
<input type="hidden" name="delFlag" id="delFlag" value="1">
  <div class="layui-form-item">
    <label class="layui-form-label">题库名称</label>
    <div class="layui-input-inline">
      <input type="text" name="name" lay-verify="required" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">题库编号</label>
    <div class="layui-input-inline">
      <input type="text" id="id" name="id" placeholder="系统生成" readonly="readonly" autocomplete="off" class="layui-input" th:value="${id}">
    </div>
  </div>
  <div class="layui-form-item">
      <label class="layui-form-label">科目</label>
      <div class="layui-input-inline">
          <select name="categoryId" id="categoryId"></select>
      </div>
  </div>
  <div class="layui-form-item">
      <label class="layui-form-label">题库标签</label>
      <div class="layui-input-inline" style="width:800px!important;">
          <div class="fairy-tag-container" style="height: 40px!important">
              <input type="text" class="fairy-tag-input tag1" autocomplete="off" value="">
          </div>
          <div style="display: none" id="tag1"></div>
      </div>
    </div>
  <div class="layui-form-item">
    <label class="layui-form-label">排序</label>
    <div class="layui-input-inline">
      <input type="number" name="orderno" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">备注说明</label>
    <div class="layui-input-inline">
      <input type="text" name="descs" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
        <label class="layui-form-label">登记人</label>
        <div class="layui-input-inline">
            <input type="text" name="createUserName" id="createUserName" th:value="${session.me.name}" readonly="readonly" class="layui-input">
            <input type="hidden" name="createUserId" id="createUserId" th:value="${session.me.id}">
        </div>
    </div>
  <div class="layui-form-item">
        <label class="layui-form-label">登记时间</label>
        <div class="layui-input-inline">
            <input type="text" name="createTime" id="createTime" th:value="${currentTime}" readonly="readonly" class="layui-input">
        </div>
    </div>
  <button id="submitBtn" class="layui-btn" style="display:none" lay-submit lay-filter="submitme">保存</button>
</form>
<script src="layuiadmin/plugins/xm-select/xm-select.js"></script>
<script>
var id = $("#id").val();

layui.use(['form', 'table', 'laydate','inputTag'], function(){
	  var form = layui.form
	  ,layer = layui.layer
	  ,inputTag = layui.inputTag

      $(".tag1").on("click",function (){
          top.layer.open({
              id:"opentag",
              type: 2,
              title:'选择标签',
              area:['1100px','600px'],
              content:  'rooter?url=ksxt/lib/select-tag',
              btn:['选择','关闭'],
              yes:function(index,layero){
                  let oldv = $("#tag").val();
                  let oldn = $("#tag1").text();
                  var iframeWin = top[layero.find('iframe')[0]['name']];
                  let ids = iframeWin.getIds();
                  let idset = new Set(),nameset = new Set();
                  if(oldv.length>0){
                      var idarr = oldv.split(",");
                      var namearr = oldn.split(",");
                      idarr.forEach(x=>idset.add(x));
                      namearr.forEach(x=>nameset.add(x))
                  }
                  console.log(">>>"+JSON.stringify([...idset]))
                  for(var i=0;i<ids.length;i++){
                      idset.add(ids[i].id+"");
                      nameset.add(ids[i].name)
                  }
                  if(idset.size===0){
                      top.layer.close(index);
                      return false;
                  }
                  console.log(JSON.stringify([...idset]))
                  $("#tag").val([...idset].join(","))
                  $(".fairy-tag-container span").remove();
                  inputTag.render({
                      elem: '.tag1',
                      data: [...nameset],
                      removeKeyNum: 8,
                      createKeyNum: 27,
                      onChange:function (v){

                          $("#tag1").text(v.join(","));
                      }
                  });
                  top.layer.close(index);
                  return false
              }
          });
      })
      select('libCategory/getPageList?page=1&limit=100' , 'categoryId',form,'name','id')
	  form.on('submit(submitme)', function(data){
			var values = {};
			$(":input").each(function(i,v){
				values[v.name] = v.value;
			})
			axios.post("lib/save",values).then(res=>{
				if(res.data.code==1){
					layer.msg(ret.msg);
				}else{
					parent.loadGrid('main-table');
					layer.msg("保存成功");
					var index = parent.layer.getFrameIndex(window.name); 
				    parent.layer.close(index); 
				}
			})
			return false; 
		  });
      let tags = [];
	  if(id){
		  loadData()

	  }
    inputTag.render({
        elem: '.tag1',
        data: tags,
        removeKeyNum: 8,
        createKeyNum: 27,
        onChange:function (v){

            $("#tag1").text(v.join(","));
        }
    });
	  function loadData(){
		var entity = getEntityInfo(id,"lib/getById");
        if(entity.tagName)
            tags = entity.tagName.split(",");
        form.val("myform",entity);
	  }
	  
})
</script>
</body>
</html>

</body>
</html>